package com.lintf.nacos.config;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;

import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.Executor;

/**
 * @author lin tingfeng
 * @ system Mac
 * @ data 2025/3/20 16:56
 * @ package_name com.lintf.nacos
 * @ project_name Default (Template) Project
 * @ desc SDK方式获取配置
 */
public class SpringNacosJDKMain {
    public static void main(String[] args) throws NacosException, IOException {
        String serverAddress = "localhost:8848";
        String dataId = "spring-nacos-config";
        String group = "DEFAULT_GROUP";

        Properties properties = new Properties();
        properties.put("serverAddr", serverAddress);
        ConfigService configService = NacosFactory.createConfigService(properties);
        String config = configService.getConfig(dataId, group, 5000);
        System.out.println(config);

        // 监听配置变化
        configService.addListener(dataId, group, new Listener() {
            @Override
            public Executor getExecutor() {
                return null;
            }

            @Override
            public void receiveConfigInfo(String configInfo) {
                System.out.println("recieve:" + configInfo);
            }
        });
        System.in.read();
    }
}